﻿function TemplateReplace(TemplateText, entity, ResultDict) {
	var boolRegex = new RegExp("\\{\\S+\\?.*\\|.*\\}", "g");
	var items = TemplateText.split(boolRegex);
	boolRegex.lastIndex = 0;
	for (var i = 0; i < items.length - 1; i += 2) {
		var groups = boolRegex.exec(TemplateText);
		var parttexts = groups[0].substr(1, groups[0].length - 2).split('?');
		var value = '';
		if (typeof ResultDict != "undefined" && ResultDict != null && typeof ResultDict[parttexts[0]] != "undefined") {
			if (ResultDict[parttexts[0]]) {
				value = parttexts[1].split('|')[0];
			}
			else {
				value = parttexts[1].split('|')[1];
			}
		}
		else {
			value = groups[0];
		}
		items.splice(i + 1, 0, value);
	}
	TemplateText = items.join('');

	var fieldRegex = new RegExp("\\[\\w+(\\.\\w+(\\(.*\\))?)?\\]", "g");
	var filedItems = TemplateText.split(new RegExp("\\[\\w+\\]|\\[\\w+\\.\\w+\\]|\\[\\w+\\.\\w+\\(.*\\)\\]", "g"));
	fieldRegex.lastIndex = 0;
	for (var i = 0; i < filedItems.length - 1; i += 2) {
		if (entity == null) {
			filedItems.splice(i + 1, 0, value);
			continue;
		}
		var groups = fieldRegex.exec(TemplateText);
		var matchedString = groups[0];
		var fieldName = matchedString.substr(1, matchedString.length - 2).split('.')[0];
		var fieldValue = typeof entity[fieldName] == "undefined" ? "" : entity[fieldName];
		var value = "";
		if (typeof groups[1] != "undefined" && groups[1] != null && groups[1] != "") {
			if (typeof groups[2] != "undefined" && groups[2] != null && groups[2] != "") {
				var strs = groups[1].split(new RegExp("\\.|\\(|\\)", "g"));
				var methodName = strs[0];
				var paras = strs[1].split(',');
				switch (methodName) {
					case "lenCut":
						{
							var len = fieldValue.toString().length;
							len = isNaN(parseInt(paras[0])) ? len : parseInt(paras[0]);
							if (len > 0 && len < fieldValue.toString().length) {
								value = "<span title=\"" + fieldValue.toString().HtmlEncode() + "\">" + fieldValue.toString().substr(0, len) + "...</span>";
							}
							else {
								value = fieldValue.toString().HtmlEncode();
							}
						}
						break;
					default:
						value = fieldValue.toString().HtmlEncode();
						break;
				}
			}
			else {
				var property = groups[1].substr(1, groups[1].length - 1);
				switch (property) {
					case "Date":
						{
							if (fieldValue instanceof Date) {
								value = fieldValue.toLocaleDateString();
							}
							else {
								value = fieldValue.toString().HtmlEncode();
							}
						}
						break;
					case "Time":
						{
							if (fieldValue instanceof Date) {
								value = fieldValue.toLocaleTimeString();
							}
							else {
								value = fieldValue.toString().HtmlEncode();
							}
						}
						break;
					default:
						value = fieldValue.toString().HtmlEncode();
						break;
				}
			}
		}
		else {
			value = fieldValue.toString().HtmlEncode();
		}
		filedItems.splice(i + 1, 0, value);
	}

	TemplateText = filedItems.join('');
	return TemplateText;
}

var RegexValidator = function(RegexText) {
	return function() {
		return new RegExp(RegexText, "g").test(this.SourceText);
	}
}

String.prototype.HtmlEncode = function() {
	var text = this.toString();
	var div = document.createElement('div');
	if (typeof div.innerText != "undefined") {
		div.innerText = text;
	} else {
		div.textContent = text;
	}
	var strHTML = div.innerHTML;
	div = null;
	return strHTML;
};

String.prototype.HtmlDecode = function() {
	var strHTML = this.toString();
	var div = document.createElement('div');
	div.innerHTML = strHTML;
	var text = "";
	if (typeof div.innerText != "undefined") {
		text = div.innerText;
	} else {
		text = div.textContent;
	}
	div = null;
	return text;
};
